package com.liugang.oss_demo.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class OSSUtil1 {

    // OSS endpoint，根据你的实际情况填写
    private static final String endpoint = "https://your-endpoint.aliyuncs.com";
    // OSS access key id，根据你的实际情况填写
    private static final String accessKeyId = "your-access-key-id";
    // OSS access key secret，根据你的实际情况填写
    private static final String accessKeySecret = "your-access-key-secret";
    // OSS bucket名称，根据你的实际情况填写
    private static final String bucketName = "your-bucket-name";

    public static void upload() {
        String fileToUpload = "/path/to/your/file.txt"; // 要上传的文件路径
        String objectName = "file.txt"; // 在 OSS 上的文件名，可以自定义

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 创建PutObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(fileToUpload));

            // 上传文件
            PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);

            // 打印ETag（上传成功时会返回）
            System.out.println("ETag: " + putObjectResult.getETag());
            System.out.println("文件上传成功");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

    public static void download() {
        String objectName = "file.txt"; // 要下载的文件在 OSS 上的文件名
        String localFilePath = "/path/to/save/file.txt"; // 下载后保存到本地的文件路径

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 创建GetObjectRequest对象
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);

            // 执行下载请求，获取OSSObject对象
            OSSObject ossObject = ossClient.getObject(getObjectRequest);

            // 获取文件流
            InputStream inputStream = ossObject.getObjectContent();

            // 将文件流保存到本地文件
            saveInputStreamToFile(inputStream, localFilePath);

            System.out.println("文件下载成功");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

    // 将输入流保存到文件
    private static void saveInputStreamToFile(InputStream inputStream, String filePath) throws IOException {
        byte[] buffer = new byte[1024];
        int bytesRead;
        FileOutputStream outputStream = new FileOutputStream(new File(filePath));
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        outputStream.close();
        inputStream.close();
    }

    public static void deleteFile() {
        String objectName = "file.txt"; // 要删除的文件在 OSS 上的文件名

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 执行删除文件请求
            ossClient.deleteObject(bucketName, objectName);

            System.out.println("文件删除成功");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }
    }

}
